package cn.buyclss.backend.service.impl.shop;

import cn.buyclss.backend.entity.domain.User;
import cn.buyclss.backend.entity.domain.shop.GoodsCategory;
import cn.buyclss.backend.entity.domain.shop.GoodsCategoryExample;
import cn.buyclss.backend.entity.po.shop.GoodsCategoryPO;
import cn.buyclss.backend.entity.po.shop.ShopPO;
import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.entity.vo.shop.GoodsCategoryVO;
import cn.buyclss.backend.mapper.shop.GoodsCategoryMapper;
import cn.buyclss.backend.service.shop.GoodsCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Cecurio
 * @create: 2019-03-12 0:31
 **/
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public RespResult page(Integer pageIndex, Integer pageSize) {
        RespResult res = RespResult.successInstance();
        // startPage(第几页, 多少条数据)
        PageHelper.startPage(pageIndex, pageSize);
        // Mybatis查询方法
        List<GoodsCategoryPO> list = goodsCategoryMapper.selectAll();
        // 用PageInfo对结果进行包装
        PageInfo<GoodsCategoryPO> pageInfo = new PageInfo<GoodsCategoryPO>(list);
        res.putValue("pages", pageInfo);
        return res;
    }

    @Override
    public RespResult getAll() {
        RespResult res = RespResult.successInstance();
        List<GoodsCategoryPO> list = goodsCategoryMapper.selectAll();
        res.putValue("categories", list);
        return res;
    }

    @Override
    public RespResult update(GoodsCategoryVO categoryVO) {

        RespResult res = RespResult.successInstance();

        GoodsCategory category = new GoodsCategory();
        category.setId(categoryVO.getId());
        category.setName(categoryVO.getName());
        category.setParentId(categoryVO.getParentId());
        category.setSortIndex(categoryVO.getSortIndex());
        category.setDeleted(categoryVO.getDeleted());

        category.setUpdatedTime(new Date());
        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        if (obj instanceof User) {
            User user = (User) obj;
            category.setUpdatedBy(user.getId());
        }
        goodsCategoryMapper.updateByPrimaryKeySelective(category);
        return res;
    }

    @Override
    public RespResult addOne(GoodsCategoryVO categoryVO) {
        RespResult res = RespResult.successInstance();

        // 检查去重
        GoodsCategoryExample example = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria cr = example.createCriteria();
        cr.andNameEqualTo(categoryVO.getName());
        List<GoodsCategory> list = goodsCategoryMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return RespResult.failureInstance("该种类已有，禁止重复添加");
        }

        GoodsCategory category = new GoodsCategory();
        category.setId(categoryVO.getId());
        category.setName(categoryVO.getName());
        category.setParentId(categoryVO.getParentId());
        category.setSortIndex(categoryVO.getSortIndex());
        category.setDeleted(0);

        category.setCreatedTime(new Date());
        category.setUpdatedTime(new Date());
        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        if (obj instanceof User) {
            User user = (User) obj;
            category.setCreatedBy(user.getId());
            category.setUpdatedBy(user.getId());
        }
        goodsCategoryMapper.insertSelective(category);
        return res;
    }

    @Override
    public RespResult deleteById(Integer categoryId) {
        RespResult res = RespResult.successInstance();
        GoodsCategory category = new GoodsCategory();
        category.setId(categoryId);
        category.setUpdatedTime(new Date());
        category.setDeleted(1);
        goodsCategoryMapper.updateByPrimaryKeySelective(category);
        return res;
    }
}
